package com.ym.pms.controller;

import com.github.pagehelper.PageInfo;
import com.ym.pms.dao.BuildingMapper;
import com.ym.pms.model.Building;
import com.ym.pms.model.Comments;
import com.ym.pms.service.BuildingService;
import com.ym.pms.utils.JSONResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "楼栋主表接口")
@RequestMapping("/buildings")
public class BuildingController {

    @Autowired
    private BuildingService service;


    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "添加楼栋")
    @PostMapping
    public JSONResult add(Building record){
        int result = service.addBu(record);
        if (result >= 1) {
            return JSONResult.ok("添加成功");
        } else{
            return JSONResult.errorMsg("添加失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "删除楼栋",notes = "根据楼栋编码删除主表对应的所有内容")
    @DeleteMapping("/delete")
    @ApiImplicitParam(name = "buCoding", value = "楼栋编码", required = true, dataType = "String" )
    public JSONResult delete(@RequestParam("buCoding")String buCoding){
        int result = service.deleteBu(buCoding);
        if (result >= 1) {
            return JSONResult.ok("删除成功");
        } else {
            return JSONResult.errorMsg("删除失败");
        }
    }

    @PreAuthorize("hasAnyRole('ADMIN','PROPERTY')")
    @ApiOperation(value = "更新楼栋表")
    @PostMapping(value = "/update")
    public JSONResult update(Building record){
        int result = service.updateBu(record);
        if (result >= 1) {
            return JSONResult.ok("更新成功");
        } else if (result == -1){
            return JSONResult.errorMsg("楼栋编码为空");
        } else if (result == 0){
            return JSONResult.errorMsg("楼栋编码");
        } else {
            return JSONResult.errorMsg("更新失败");
        }
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询楼栋主表全部信息",notes = "分页查询出楼栋主表所有内容")
    @GetMapping("/findAllBuilding")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findAllBuilding(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo all = service.findAll(pageNum, pageSize);
        return JSONResult.ok(all);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "根据coding查询楼栋")
    @GetMapping("/findByCoding")
    @ApiImplicitParam(name = "bu_Coding", value = "楼栋编号", required = true, dataType = "String" )
    public JSONResult findByCoding(@RequestParam("bu_Coding")String bu_Coding){
        Building building = service.findByCoding(bu_Coding);
        return JSONResult.ok(building);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页查询楼栋主表用于展示的信息",notes = "包含{楼栋编码、楼栋名称、项目id、项目名称、项目位置、区域、物业地址、总面积}")
    @GetMapping("/findAllOfDisplay")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult findAllOfDisplay(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.displayInformation(pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    @PreAuthorize("hasAnyRole('USER','ADMIN','ARCHIVIST','PROPERTY','BASIC')")
    @ApiOperation(value = "分页模糊查询楼栋表信息")
    @GetMapping("/fuzzyQuery")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int" ),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int" )
    })
    public JSONResult fuzzyQuery(Building building, @RequestParam("pageNum")int pageNum, @RequestParam("pageSize")int pageSize){
        PageInfo pageInfo = service.fuzzyQuery(building, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }


}
